package com.kbbywyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.widget.divider.VerticalItemDecoration;
import java.util.List;

/**
 * Created by Jiangzx on 2017/9/7 17:16.
 */

public class BankFilterPop<T> extends PopupWindow {
    public interface OnFilterSelected{
        void onFilterSelected(Object selected);
    }
    private OnFilterSelected listener;
    public BankFilterPop(Context context,@NonNull List<T> data,OnFilterSelected listener){
        final LayoutInflater inflater=LayoutInflater.from(context);
        View root= inflater.inflate(R.layout.pop_bank_filter,null);
        RecyclerView recyclerView= (RecyclerView) root.findViewById(R.id.recycle_view);
        recyclerView.getLayoutParams().height= (int) (StringUtil.getScreenHeight()*0.4f);
        root.findViewById(R.id.view_area_dismiss).setOnClickListener(v->dismiss());
        setBackgroundDrawable(new ColorDrawable());
        this.listener=listener;
        FilterAdapter<T> adapter=new FilterAdapter<>(data);
        recyclerView.setLayoutManager(
                new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        recyclerView.addItemDecoration(new VerticalItemDecoration(Constants.DIVIDER_COLOR,1));
        recyclerView.setAdapter(adapter);
        setAnimationStyle(R.style.TopInTopOutStyle);
        setContentView(root);
    }

    private class FilterHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        private ImageView iv;
        private FilterHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.tv);
            iv= (ImageView) itemView.findViewById(R.id.iv);
        }
    }

    private class FilterAdapter<T> extends RecyclerView.Adapter{
        private int selected=0;
        private List<T> data;
        FilterAdapter(List<T> data){
            this.data=data;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new FilterHolder(LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_bank_filter,parent,false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            FilterHolder fHolder= (FilterHolder) holder;
            final T t=data.get(position);
            fHolder.tv.setText(t.toString());
            if(position==selected){
                fHolder.tv.setTextColor(0XFF14BF95);
                fHolder.iv.setVisibility(View.VISIBLE);
            }else {
                fHolder.tv.setTextColor(0XFF333333);
                fHolder.iv.setVisibility(View.INVISIBLE);
            }
            holder.itemView.setOnClickListener(v -> {
                selected=position;
                notifyDataSetChanged();
                dismiss();
                if(listener!=null){
                    listener.onFilterSelected(t);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data==null?0:data.size();
        }
    }
}
